//
//  DetailStoryTableViewCell.m
//  EDPS
//
//  Created by 张维扬 on 15/11/27.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "DetailStoryTableViewCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
@interface DetailStoryTableViewCell ()
@property (nonatomic, retain) UIImageView *cover_image; // 图片
@property (nonatomic, retain) UILabel *text;
@property (nonatomic, retain) UIImageView *triangle; // 小三角
@end

@implementation DetailStoryTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.contentView.backgroundColor = RGBA(253, 244, 233, 1);
    self.cover_image = [[UIImageView alloc] init];
    [self.contentView addSubview:self.cover_image];
    
    self.text = [[UILabel alloc] init];
    [self.text setFont:[UIFont systemFontOfSize:15]];
    self.text.numberOfLines = 0;
    [self.contentView addSubview:self.text];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.cover_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(10);
        make.left.mas_equalTo(self.contentView.mas_left).offset(20);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-20);
        // 长 X 宽 = 面积 -->  面积 / 宽 = 长
        make.size.mas_equalTo(CGSizeMake(ScreenWidth - 40, self.model.photo_height.integerValue * (ScreenWidth - 40) / self.model.photo_width.integerValue));
    }];
    
    // 计算出文本高度
    CGFloat height = [[self class] heightWithText:self.model.text fontSize:15 width: ScreenWidth - 40];
    NSLog(@"%f", height);
    [self.text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.cover_image.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView.mas_left).offset(20);
        make.size.mas_equalTo(CGSizeMake(ScreenWidth - 40, height));
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];
}
// 赋值
- (void)setModel:(ExploreModel *)model
{
    if (_model != model) {
        _model = model;
    }
    [self.cover_image sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"hold"]];
    self.text.text = self.model.text;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
